' Stephen Toub
' Coded and published in January 2007 issue of MSDN Magazine 
' http://msdn.microsoft.com/msdnmag/issues/07/01/PreviewHandlers/default.aspx


Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Resources
Imports System.Collections
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports C4F.DevKit.PreviewHandler.PreviewHandlerFramework

Namespace C4F.DevKit.PreviewHandler.PreviewHandlers
    <PreviewHandler("MSDN Magazine RESX Preview Handler", ".resx", "{A35B7E1D-C922-439d-81F4-6F9681D02F7F}"), ProgId("C4F.DevKit.PreviewHandler.PreviewHandlers.ResxPreviewHandler"), Guid("F2D43DD7-1233-4d5f-9032-B82A570AEE81"), ClassInterface(ClassInterfaceType.None), ComVisible(True)> _
    Public NotInheritable Class ResxPreviewHandler
        Inherits FileBasedPreviewHandler
        Protected Overrides Function CreatePreviewHandlerControl() As PreviewHandlerControl
            Return New ResxPreviewHandlerControl()
        End Function

        Private NotInheritable Class ResxPreviewHandlerControl
            Inherits StreamBasedPreviewHandlerControl
            Public Overrides Sub Load(ByVal previewStream As Stream)
                Dim listView As ListView = New ListView()

                listView.Columns.Add("File Name", -2)
                listView.Columns.Add("Data Type", -2)
                listView.Columns.Add("Value", -2)

                listView.Dock = DockStyle.Fill
                listView.BorderStyle = BorderStyle.None
                listView.FullRowSelect = True
                listView.HeaderStyle = ColumnHeaderStyle.Nonclickable
                listView.MultiSelect = False
                listView.View = View.Details

                Environment.CurrentDirectory = Path.GetDirectoryName((CType(previewStream, FileStream)).Name)
                Using reader As ResXResourceReader = New ResXResourceReader(previewStream)
                    For Each entry As DictionaryEntry In reader
                        Dim item As ListViewItem = New ListViewItem(New String() {entry.Key.ToString(), entry.Value.GetType().ToString(), entry.Value.ToString()})
                        item.Tag = entry
                        listView.Items.Add(item)
                    Next entry
                End Using

                Controls.Add(listView)
            End Sub
        End Class
    End Class
End Namespace